#################################
#Figure 2
#################################

library(foreign)
library(lattice)
library(Hmisc)
library(readstata13)
library(latticeExtra)

data<-read.dta13("Figure2a_Preds_reshaped.dta")
data2<-read.dta13("Figure2b_Preds_reshaped.dta")
days<-data[,1]
CycleLowBound1<-data[,2]
CycleUpBound1<-data[,3]
CycleLowBound32<-data[,4]
CycleUpBound32<-data[,5]
PostLowBound1<-data2[,2]
PostUpBound1<-data2[,3]
PostLowBound32<-data2[,4]
PostUpBound32<-data2[,5]

group<-c("Linear Cycle Trend", "Post-1994 Election Indicator")

x<-1:735

pdf(file="Figure2.pdf", family="Bookman")
setTrellis()
xyplot(CycleLowBound1 ~ days | group, type="l", xlab='Days to Next Election', ylab='Probability of Casting Party Vote', xlim=rev(range(x)), ylim=c(.65, 1), col=c("white"), key=list(text=list(c("Early in Time Series", "Late In Time Series")), points=list(pch=c(15, 15), col=c("gray35", "gray70")), columns=1))

a<-c(days, rev(days))
b<-c(CycleLowBound1, rev(CycleUpBound1))
c<-c(CycleLowBound32, rev(CycleUpBound32))
d<-c(PostLowBound1, rev(PostUpBound1))
e<-c(PostLowBound32, rev(PostUpBound32))

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.polygon", list(x=a, y=b, col="gray35", border=NA))
trellis.unfocus( )

trellis.focus("panel", 1, 1, highlight=FALSE)
do.call("panel.polygon", list(x=a, y=c, col="gray70", border=NA))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.polygon", list(x=a, y=d, col="gray35", border=NA))
trellis.unfocus( )

trellis.focus("panel", 2, 1, highlight=FALSE)
do.call("panel.polygon", list(x=a, y=e, col="gray70", border=NA))
trellis.unfocus( )

dev.off()
